package com.shxy.service;

import com.shxy.dao.INodeDao;
import com.shxy.dao.IMaterialDao;
import com.shxy.pojo.Material;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class MaterialService implements IMaterialService {
    @Value("${my.resources.root}")
    private String rootPath;
    @Autowired
    private IMaterialDao materialDao;
    @Autowired
    private INodeDao catalogDao;

    @Override
    public List<Material> importMaterials(String path, String suffix) throws IOException {
        List<Material> materials = new ArrayList<>();
        //获取文件列表
        Collection<File> files = FileUtils.listFiles(new File(path), FileFilterUtils.suffixFileFilter(suffix), DirectoryFileFilter.INSTANCE);
        //循环将文件转换成资源并保存
        for (File file: files) {
            //获取文件信息
            Material material = file2Material(file);
            //如果该文件不存在存在
            if(!materialDao.existsByMd5hex(material.getMd5hex())){
                //保存资源
                materialDao.save(material);
                //添加到返回列表
                materials.add(material);
            }
        }
        return materials;
    }

    @Override
    public String getFileType(File file) {
        String mtype = "";
        //判断后缀名
        switch (getFileSuffix(file.getName()).toLowerCase()){
            case "mp4": mtype = "video"; break;
            case "jpg": mtype = "image"; break;
        }
        return mtype;
    }

    @Override
    public String getFileMd5(File file) throws IOException {
        return DigestUtils.md5Hex(new FileInputStream(file));
    }

    @Override
    public Material file2Material(File file) throws IOException {
        Material material = new Material();
        material.setMd5hex(getFileMd5(file));
        material.setCoverImage("");
        material.setName(file.getName());
        material.setMType(getFileType(file));
        material.setAddTime(new Date());
        material.setPath(file.getPath());
        material.setStatus(0);
        material.setUrl("");
        return material;
    }

    @Override
    public String getFileSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    @Override
    public Material uploadFile(MultipartFile file) {
        return null;
    }

    @Override
    public int refCount(int materialId) {
        return 0;
    }

    @Override
    public List<Material> search(String name, String mtype) {
        return null;
    }
}
